package com.sunlg.smarthome.ui.plant;

import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.BaseActivity;
import com.sunlg.smarthome.contract.PumpCtrlContract;
import com.sunlg.smarthome.contract.presenter.PumpCtrlPresenter;
import com.sunlg.smarthome.data.entity.Pump;

import butterknife.BindView;

public class PumpActivity extends BaseActivity
        implements PumpCtrlContract.View, View.OnClickListener{

    @BindView(R.id.v_ctrl)
    View vCtrl;
    @BindView(R.id.btn_start)
    Button btnStart;
    @BindView(R.id.btn_stop)
    Button btnStop;
    @BindView(R.id.sb_pump)
    SeekBar seekBar;

    private Pump pump;
    private PumpCtrlContract.Presenter presenter;
    private int status;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_pump;
    }

    @Override
    protected void setupToolBar(Toolbar toolbar) {
        super.setupToolBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("水泵");
        actionBar.setDisplayHomeAsUpEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doFinish();
            }
        });
    }

    @Override
    protected void init() {
        pump = (Pump) getIntent().getExtras().getSerializable("pump");
        presenter = new PumpCtrlPresenter();
        presenter.attachView(this);
    }

    @Override
    protected void initView() {
        if(pump.getStatus() > 0){
            vCtrl.setVisibility(View.INVISIBLE);
            seekBar.setProgress(pump.getStatus());
        }else {
            vCtrl.setVisibility(View.VISIBLE);
        }
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                status = progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                presenter.startPump(pump.getId(), status);
            }
        });

        btnStart.setOnClickListener(this);
        btnStop.setOnClickListener(this);
    }

    @Override
    public void startPumpSuccess() {
        seekBar.setProgress(status);
        pump.setStatus(status);
        if(vCtrl.getVisibility() == View.VISIBLE){
            vCtrl.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void stopPumpSuccess() {
        seekBar.setProgress(0);
        if(vCtrl.getVisibility() != View.VISIBLE){
            vCtrl.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showError(String msg) {
        toast(msg);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_start){
            if(pump.getStatus() <= 0){
                presenter.startPump(pump.getId(), 50);
                status = 50;
            }else {
                presenter.startPump(pump.getId(), pump.getStatus());
                status = pump.getStatus();
            }

        }else if(v.getId() == R.id.btn_stop){
            presenter.stopPump(pump.getId());
        }
    }
}
